package com.kai.service.problem.controller;

import com.kai.service.data.PagingResult;
import com.kai.service.data.Result;
import com.kai.service.data.request.problem.*;
import com.kai.service.data.response.problem.CountUserProblemResponse;
import com.kai.service.data.response.problem.ProblemReplyResponse;
import com.kai.service.data.response.problem.ProblemResponse;
import com.kai.service.problem.facade.ProblemFacade;
import com.kai.service.util.ResultResponseUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/problem")
@Tag(name = "问答")
public class ProblemController {

    @Autowired
    private ProblemFacade problemFacade;

    /**
     * 创建问题
     *
     * @param createProblemRequest
     * @return
     */
    @PostMapping
    @Operation(summary = "创建问题")
    public Result<Boolean> createProblem(@RequestBody CreateProblemRequest createProblemRequest) {
        return ResultResponseUtil.success(problemFacade.createProblem(createProblemRequest));
    }

    /**
     * 分页查询问题
     *
     * @param pagingProblemRequest
     * @return
     */
    @PostMapping("/paging")
    @Operation(summary = "分页查询问题")
    public PagingResult<ProblemResponse> pagingProblem(@RequestBody PagingProblemRequest pagingProblemRequest) {
        return ResultResponseUtil.pageSuccess(problemFacade.pagingProblem(pagingProblemRequest));
    }

    /**
     * 查询热门问题列表
     *
     * @return
     */
    @GetMapping("/hot:getList")
    @Operation(summary = "查询热门问题列表")
    public Result<List<ProblemResponse>> hotProblemList(@RequestParam("userId") Long userId){
        return ResultResponseUtil.success(problemFacade.hotProblemList(userId));
    }

    /**
     * 修改问题
     *
     * @param problemId
     * @param updateProblemRequest
     * @return
     */
    @PutMapping("/{problemId}")
    @Operation(summary = "修改问题")
    public Result<Boolean> updateProblem(@PathVariable("problemId") Integer problemId, @RequestBody UpdateProblemRequest updateProblemRequest) {
        return ResultResponseUtil.success(problemFacade.updateProblem(problemId, updateProblemRequest));
    }

    /**
     * 删除问题
     *
     * @param problemId
     * @return
     */
    @DeleteMapping("/{problemId}")
    @Operation(summary = "删除问题")
    public Result<Boolean> deleteProblem(@PathVariable("problemId") Integer problemId) {
        return ResultResponseUtil.success(problemFacade.deleteProblem(problemId));
    }

    /**
     * 回答问题
     *
     * @param problemId
     * @param replyProblemRequest
     * @return
     */
    @PostMapping("/reply/{problemId}")
    @Operation(summary = "回答问题")
    public Result<Boolean> replyProblem(@PathVariable("problemId") Integer problemId, @RequestBody ReplyProblemRequest replyProblemRequest) {
        return ResultResponseUtil.success(problemFacade.replyProblem(problemId, replyProblemRequest));
    }

    /**
     * 分页查询问题回复
     *
     * @param problemId
     * @param pagingProblemReplyRequest
     * @return
     */
    @PostMapping("/reply/{problemId}:paging")
    @Operation(summary = "分页查询问题回复")
    public PagingResult<ProblemReplyResponse> pagingProblemReply(@PathVariable("problemId") Long problemId, @Valid @RequestBody PagingProblemReplyRequest pagingProblemReplyRequest) {
        return ResultResponseUtil.pageSuccess(problemFacade.pagingProblemReply(problemId, pagingProblemReplyRequest));
    }

    /**
     * 问题操作，点赞等
     *
     * @param problemOperatorRecordRequest
     * @return
     */
    @PostMapping("/operator")
    @Operation(summary = "问题操作，点赞等")
    public Result<Boolean> operatorProblem(@Valid @RequestBody ProblemOperatorRecordRequest problemOperatorRecordRequest) {
        return ResultResponseUtil.success(problemFacade.operatorProblem(problemOperatorRecordRequest));
    }

    /**
     * 删除回答
     *
     * @param replyId
     * @return
     */
    @DeleteMapping("/reply/{replyId}")
    @Operation(summary = "删除回答")
    public Result<Boolean> deleteReply(@PathVariable("replyId") Integer replyId) {
        return ResultResponseUtil.success(problemFacade.deleteReply(replyId));
    }

    /**
     * 统计用户提问数量
     *
     * @param userId
     * @return
     */
    @GetMapping("/count/{userId}")
    @Operation(summary = "统计用户提问数量")
    public Result<CountUserProblemResponse> countProblem(@PathVariable("userId") Long userId){
        return ResultResponseUtil.success(problemFacade.countProblem(userId));
    }

}
